/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.actions;

import com.ejb.UsuarioFachadaRemote;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author victor
 */
public class ModificarPassAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "ExitoModificarPass";

    private ModificarPassForm miForm;
    private UsuarioFachadaRemote UsuarioFachadaBean;

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        miForm = (ModificarPassForm) form;
        String pass1 = miForm.getpass1();

        String nick = (String) request.getSession().getAttribute("Nick");

        // Llamada al Enterprise Bean
        UsuarioFachadaBean = lookupUsuarioFachadaBean();



        UsuarioFachadaBean.CambiarPass(nick, pass1);

        request.getSession().setAttribute("Pass", pass1);


        return mapping.findForward(SUCCESS);

    }

    private UsuarioFachadaRemote lookupUsuarioFachadaBean() {
        try {
            Context c = new InitialContext();
            return (UsuarioFachadaRemote) c.lookup("java:comp/env/UsuarioFachadaBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
